#include "stdafx.h"
#include "MVData.h"
#include "MVDataBlockP.h"
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	cout << "reading file... ";
	// 打开一个已经存在的XML文件，并读入数据
	CMVData * pDataReader = new CMVData( TYPE_MVDATA_XML, "testfile.xml" );
	cout << " done!" << endl;

	if( NULL == pDataReader || !pDataReader->IsDataReady() )
	{
		cerr << "error in open xml file" << endl;
		return -1;
	}
	

	CMVData * pDataWriter = new CMVData();
	pDataWriter->CreateNew( TYPE_MVDATA_XML, "testfile2.xml" );

	if( NULL == pDataWriter || !pDataWriter->IsDataReady() )
	{
		cerr << "error in open xml file" << endl;
		return -1;
	}
	pDataWriter->Clear();

	int frame_count = pDataReader->GetFrameCount();
	for (int i = 0; i < frame_count; i++)
	{
		pDataReader->ReadFrame(i);
		pDataWriter->WriteFrame(i);
		pDataWriter->SetFrameSize(pDataReader->GetFrameWidth(), pDataReader->GetFrameHeight());
		const CMVDataMB * pDataReaderMB = new CMVDataMB(TYPE_MB_SKIP, 0, 0);
		pDataReaderMB = pDataReader->GetNextMB(NULL, i);


		int mb_count = pDataReader->GetMBCount(i);
		for (int j = 0; j < mb_count; j++)
		{
			CMVDataMB * pDataWriterMB = new CMVDataMB(TYPE_MB_SKIP, 0, 0);
				pDataWriterMB->SetMBType(pDataReaderMB->GetMBType());
			pDataWriterMB->SetCoordinate(pDataReaderMB->GetCoordinateX(), pDataReaderMB->GetCoordinateY());
			if (pDataReaderMB->HaveMVCoordinate())
				pDataWriterMB->SetMVCoordinate(pDataReaderMB->GetMVCoordinateX(), pDataReaderMB->GetMVCoordinateY());
			if (pDataReaderMB->HaveASAD())
				pDataWriterMB->SetASAD(pDataReaderMB->GetASAD());
			if (pDataReaderMB->HaveSADBitCount())
				pDataWriterMB->SetSADBitCount(pDataReaderMB->GetSADBitCount());
			const CMVDataBlock * pDataReaderBlock = new CMVDataBlockP();
			pDataReaderBlock = pDataReaderMB->GetNextBlock(NULL);
			CMVDataBlock * pDataWriterBlock = new CMVDataBlockP();
			switch (pDataReaderMB->GetMBType())
			{
			case TYPE_MB_P16x8:
				for (int k = 0; k < 2; k++)
				{
					if (pDataReaderBlock == NULL)
						break;
					pDataWriterBlock->SetFirstLayerLoc(pDataReaderBlock->GetFirstLayerLoc());
					if (pDataReaderBlock->HaveMVCoordinate())
						pDataWriterBlock->SetMVCoordinate(pDataReaderBlock->GetMVCoordinateX(), pDataReaderBlock->GetMVCoordinateY());
					if (pDataReaderBlock->HaveASAD())
						pDataWriterBlock->SetASAD(pDataReaderBlock->GetASAD());
					if (pDataReaderBlock->HaveSADBitCount())
						pDataWriterBlock->SetSADBitCount(pDataReaderBlock->GetSADBitCount());
					pDataReaderBlock = pDataReaderMB->GetNextBlock(pDataReaderBlock);
					pDataWriterMB->AddBlock(pDataWriterBlock);
				}
				break;
				//依次把left和right两个sub_mb从pDataReaderBlock复制到pDataWriterBlock
			case TYPE_MB_P8x16:
				for (int k = 0; k < 2; k++)
				{
					if (pDataReaderBlock == NULL)
						break;
					pDataWriterBlock->SetFirstLayerLoc(pDataReaderBlock->GetFirstLayerLoc());
					if (pDataReaderBlock->HaveMVCoordinate())
						pDataWriterBlock->SetMVCoordinate(pDataReaderBlock->GetMVCoordinateX(), pDataReaderBlock->GetMVCoordinateY());
					if (pDataReaderBlock->HaveASAD())
						pDataWriterBlock->SetASAD(pDataReaderBlock->GetASAD());
					if (pDataReaderBlock->HaveSADBitCount())
						pDataWriterBlock->SetSADBitCount(pDataReaderBlock->GetSADBitCount());
					pDataReaderBlock = pDataReaderMB->GetNextBlock(pDataReaderBlock);
					pDataWriterMB->AddBlock(pDataWriterBlock);
				}
				break;
				//依次把up和down两个sub_mb从pDataReaderBlock复制到pDataWriterBlock
			case TYPE_MB_P8x8:
				while ( pDataReaderBlock != NULL )
				{
					if ( pDataReaderBlock->HaveSubMBType() )
					{
						pDataWriterBlock->SetFirstLayerLoc( pDataReaderBlock->GetFirstLayerLoc() );
						pDataWriterBlock->SetSubMBType( pDataReaderBlock->GetSubMBType() );

					}
						pDataWriterBlock->SetAllLayerLoc( pDataReaderBlock->GetFirstLayerLoc(), pDataReaderBlock->GetSecondLayerLoc() );
					if ( pDataReaderBlock->HaveMVCoordinate() )
						pDataWriterBlock->SetMVCoordinate( pDataReaderBlock->GetMVCoordinateX(), pDataReaderBlock->GetMVCoordinateY() );
					if ( pDataReaderBlock->HaveASAD() )
						pDataWriterBlock->SetASAD( pDataReaderBlock->GetASAD() );
					if ( pDataReaderBlock->HaveSADBitCount() )
						pDataWriterBlock->SetSADBitCount( pDataReaderBlock->GetSADBitCount() );
					pDataReaderBlock = pDataReaderMB->GetNextBlock(pDataReaderBlock);
					pDataWriterMB->AddBlock(pDataWriterBlock);
				}
				
				break;

			default:
				break;
			}
			pDataWriter->AddMB(pDataWriterMB, i);
			pDataReaderMB = pDataReader->GetNextMB(pDataReaderMB, i);
			if (pDataReaderMB == NULL)
				break;
		}
		pDataWriter->SetFrameType(pDataReader->GetFrameType(i), i);
	}
	pDataReader->Close();
	pDataWriter->Close();
	return 0;
}
